package com.coocaa.ops.admin.v1.cornericontype.biz;

import com.coocaa.ops.admin.v1.cornericon.mapper.IGlobalCornerIconMapper;
import com.coocaa.ops.admin.v1.cornericontype.mapper.IGlobalCornerIconTypeMapper;
import com.coocaa.ops.common.core.base.BaseBizImpl;
import com.coocaa.ops.common.core.base.mybatis.QueryTemplate;
import com.coocaa.ops.common.core.base.mybatis.UpdateTemplate;
import com.coocaa.ops.common.core.biz.cornericon.entity.GlobalCornerIconEntity;
import com.coocaa.ops.common.core.biz.cornericontype.entity.GlobalCornerIconTypeEntity;
import com.coocaa.ops.common.tool.date.DateUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author bijiahao
 * @date : 2018/8/17.
 * @description
 */
@Service
public class CornerIconTypeBizImpl extends BaseBizImpl<IGlobalCornerIconTypeMapper, GlobalCornerIconTypeEntity> implements ICornerIconTypeBiz {
    @Autowired
    private IGlobalCornerIconMapper globalCornerIconMapper;
    /**
     * 批量 逻辑删除
     *
     * @param ids
     */
    @Override
    public void logicDelBatchByIds(Integer[] ids) {
        UpdateTemplate<GlobalCornerIconTypeEntity> wrapper = UpdateTemplate.getUpdateTemplate();
        wrapper.in("id", ids);
        GlobalCornerIconTypeEntity update = new GlobalCornerIconTypeEntity();
        update.setFlag(0);
        update.setLastUpdateDate(DateUtil.getCurDate());
        getMapper().update(update, wrapper);
    }

    /**
     * 根据类型名称查询
     *
     * @param typeName
     * @return
     */
    @Override
    public GlobalCornerIconTypeEntity queryByTypeName(String typeName) {
        QueryTemplate<GlobalCornerIconTypeEntity> wrapper = new QueryTemplate<>();
        wrapper.eq("type_name", typeName);
        List<GlobalCornerIconTypeEntity> result = getMapper().selectList(wrapper);
        return CollectionUtils.isNotEmpty(result) ? result.get(0) : null;
    }

    @Override
    public boolean isIconTypeHasUsed(Integer id) {
        QueryTemplate<GlobalCornerIconEntity> queryTemplate = QueryTemplate.getEqQueryTemplate("flag", 1);
        queryTemplate.lambda().eq(GlobalCornerIconEntity::getTypeId, id);
        Integer count = globalCornerIconMapper.selectCount(queryTemplate);
        return null != count && count > 0;
    }
}
